<?php

require_once 'UC_DBBase.php';
require_once 'UC_Table.php';
require_once 'UC_TableCollection.php';

class UC_Database extends UC_DBBase
{
    public function __construct()
    {
        $this->Path_ = new UC_DBObjectPath();
    }
            
    public function Associators_()
    {
        
    }
            
    public function Clone_()
    {
        
    }
            
    public function CompareTo_()
    {
        
    }
            
    public function Delete_()
    {
        
    }
            
    public function ExecuteMethod_()
    {
        
    }
            
    static function Exists_(UC_DBObjectPath $object_path)
    {
        if($object_path->get_is_Database())
        {
            try
            {
                $sql = "Select SCHEMA_NAME From INFORMATION_SCHEMA.SCHEMATA" .
                    " Where SCHEMA_NAME = '" . $object_path->get_Name() . "'";

                if($object_path->get_Server()->get_PDO()->query($sql)->rowCount() == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (PDOException $e)
            {
                throw new UC_DBException(
                    'Unable to determine if the database exists.');
            }
        }
        else
        {
            return false;
        }
    }
    
    static function Get_(UC_DBObjectPath $object_path)
    {
        if($object_path->get_is_Database())
        {
            if(self::Exists_($object_path))
            {
                $db = new UC_Database();
                $db->set_Path_($object_path);
                return $db;
            }
            else
            {
                throw new InvalidArgumentException(
                    'Invalid object path.');
            }
        }
        else 
        {
            throw new InvalidArgumentException(
                'Invalid object path.');
        }
    }
            
    static function Instances_()
    {
        
    }
            
    public function Put_()
    {
        
    }
            
    public function References_()
    {
        
    }
            
    static function SpawnInstance_()
    {
        
    }
    
    public function get_Name()
    {
        return $this->get_Path_()->get_Name();
    }

    public function set_Name($Name)
    {
        if(is_string($Name))
        {
            $this->get_Path_()->set_Name($Name);
        }
        else
        {
            throw new InvalidArgumentException(
                'The name should be a string.');
        }
    }

    public function get_Server()
    {
        return $this->get_Path_()->get_Server();
    }

    public function set_Server(UC_DBServer $Server)
    {
        $this->get_Path_()->set_Server($Server);
    }
    
    public function get_Tables()
    {
        if(!$this->Tables instanceof UC_TableCollection)
        {
            $this->Tables = new UC_TableCollection();
            $this->PopulateTables();
        }
        
        return $this->Tables;
    }

    public function set_Tables($Tables)
    {
        $this->Tables = $Tables;
    }

    public function get_CharacterSet()
    {
        return $this->CharacterSet;
    }

    public function set_CharacterSet($CharacterSet)
    {
        $this->CharacterSet = $CharacterSet;
    }

    public function get_Collation()
    {
        return $this->Collation;
    }

    public function set_Collation($Collation)
    {
        $this->Collation = $Collation;
    }

    public function ScriptCreate()
    {
        $sql = "CREATE DATABASE `" . $this->get_Name() . "`" . PHP_EOL .
            "\t" . "CHARACTER SET " . $this->CharacterSet . PHP_EOL .
            "\t" . "COLLATE " . $this->Collation . ";" . PHP_EOL;
      
        return $sql;
    }
    
    public function ScriptAlter()
    {
        $sql = "ALTER DATABASE `" . $this->get_Name() . "`" . PHP_EOL .
            "\t" . "CHARACTER SET " . $this->CharacterSet . PHP_EOL .
            "\t" . "COLLATE " . $this->Collation . ";" . PHP_EOL;
      
        return $sql;
    }
    
    private function PopulateTables()
    {
        $sql = "Select TABLE_NAME From INFORMATION_SCHEMA.TABLES " .
            "Where TABLE_SCHEMA = '" . $this->Path_->get_Schema() . "'";
        
        try
        {
            foreach($this->get_Path_()->get_Server()->get_PDO()->query($sql) as $t)
            {
                $path = new UC_DBObjectPath();
                $path->set_is_Table(true);
                $path->set_Schema($this->get_Path_()->get_Schema());
                $path->set_Name($t['TABLE_NAME']);
                $path->set_Table($t['TABLE_NAME']);
                $path->set_Server($this->get_Path_()->get_Server());

                $table = UC_Table::Get_($path);
                $this->Tables->add($table);
            }
        }
        catch (PDOException $e)
        {
            throw new UC_DBException('Unable to enumerate tables.');
        }
    }
    
    private $Tables;
    
    private $CharacterSet = 'latin1';
    private $Collation = 'latin1_swedish_ci';
}


?>
